package com.example.kaogong.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.kaogong.common.utils.RespBean;
import com.example.kaogong.common.utils.SensitiveWordFilter;
import com.example.kaogong.sys.mapper.UserMapper;
import com.example.kaogong.sys.pojo.Comment;
import com.example.kaogong.sys.mapper.CommentMapper;
import com.example.kaogong.sys.pojo.CourseComment;
import com.example.kaogong.sys.pojo.User;
import com.example.kaogong.sys.pojo.UserLike;
import com.example.kaogong.sys.pojo.dto.PostCommentDto;
import com.example.kaogong.sys.pojo.dto.PostDto;
import com.example.kaogong.sys.pojo.vo.PostCommentVo;
import com.example.kaogong.sys.pojo.vo.PostVo;
import com.example.kaogong.sys.service.ICommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.time.LocalDateTime;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author    
 * @since 2022-03-26
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private SensitiveWordFilter sensitiveWordFilter;

    @Override
    public RespBean addPostComment(PostCommentDto postCommentDto, String name) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
        if (null == user)
            return RespBean.error();

        Comment comment = new Comment();
        comment.setPostId(postCommentDto.getPostId());
        // 敏感词过滤
        comment.setContent(sensitiveWordFilter.filter(postCommentDto.getContent()));
        comment.setPostTime(LocalDateTime.now());
        comment.setUid(user.getUid());
        comment.setReplyId(postCommentDto.getReplyId());
        comment.setTargetId(postCommentDto.getTargetId());
        int insert = commentMapper.insert(comment);
        if (insert < 1)
            return RespBean.error();
        return RespBean.success();
    }

    @Override
    public RespBean<Page<PostCommentVo>> getPostComment(Long current, Long size, Integer postId, Integer cid) {
        System.out.println("hello");
        Page<PostCommentVo> postCommentVoPage = commentMapper.getPostComment(new Page<Comment>(current, size), postId, cid);
        postCommentVoPage.getRecords().forEach(postCommentVo -> {
            //回复数
            Integer count = commentMapper.selectCount(new QueryWrapper<Comment>().eq("reply_id", postCommentVo.getCid()));
            postCommentVo.setCount(count);
        });
        return RespBean.success(postCommentVoPage);
    }

    @Override
    public RespBean shieldComment(Integer commentId) {
        int row = commentMapper.shieldComment(commentId);
        if (row < 1)
            return RespBean.error();
        return RespBean.success();
    }

    @Override
    public RespBean deleteComment(Integer commentId, String name) {
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
        if (null == user)
            return RespBean.error();
        int delete = commentMapper.delete(new QueryWrapper<Comment>().eq("cid", commentId).eq("uid", user.getUid()));
        if (delete < 1)
            return RespBean.error();
        return RespBean.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RespBean likeComment(Integer commentId, String name) {
        try {
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", name));
            if (null == user)
                return RespBean.error();

            UserLike userLike = new UserLike(user.getUid(), commentId);
            int row = commentMapper.insertUserPostCommentLike(userLike);
            if (row < 1)
                return RespBean.error();
            int addLike = commentMapper.likeComment(commentId);
            if (addLike < 1)
                return RespBean.error();

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return RespBean.error();
        }
        return RespBean.success();
    }
}
